package com.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.text.SimpleDateFormat;
public class JackUtil {
    static ObjectMapper om = new ObjectMapper();
    public static String objecToString(Object o){
        String str = "";
        try {
             str =  om.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
    //日期
    //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //objectMapper.setDateFormat(sdf)
    public static String objecToDateString(Object o,String date){
        SimpleDateFormat sdf = new SimpleDateFormat(date);
        om.setDateFormat(sdf);
        String str = "";
        try {
            str =  om.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
    //根据字符串(json)转相应的对象
    public static Object stringToObject(String info,Class cla){
        Object obj = null;
        try {
             obj = om.readValue(info,cla);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return obj;
    }


}
